﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OnlineEducation.Controllers;
using OnlineEducation.Data;
using OnlineEducation.Extensions;
using OnlineEducation.Models.ProfileViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace OnlineEducation.Areas.Profile.Controllers
{
    [Area("Profile")]
    [Route("[controller]/[action]")]
    [Authorize(Roles = "Student,Teacher")]
    public class ProfileController : BaseController
    {

        private readonly ILogger _logger;
        private readonly ApplicationDbContext _context;
        private readonly IWebHostEnvironment _env;
     

        public ProfileController(ApplicationDbContext context, ILogger<ProfileController> logger, IWebHostEnvironment env)
        {
            _context = context;
            _logger = logger;
            _env = env;
   
        }
        public IActionResult Index()
        {
            var viewModel = new ProfileViewModel();
          
            var userinfo = _context.ApplicationUsers.Find(User.GetUserID());
            viewModel.Address = userinfo.Address;
            viewModel.BirthDate = userinfo.BirthDate;
            viewModel.CreatedTime = userinfo.CreatedTime;
            viewModel.Email = userinfo.Email;
            viewModel.FullName = userinfo.FullName;
            viewModel.Gender = userinfo.Gender;
            viewModel.Intro = userinfo.Intro;
            viewModel.PhoneNumber = userinfo.PhoneNumber;
            viewModel.QQ = userinfo.QQ;
            viewModel.Signature = userinfo.Signature;
            viewModel.Status = userinfo.Status;
            viewModel.Title = userinfo.Title;
            viewModel.WeChat = userinfo.WeChat;
            viewModel.Weibo = userinfo.Weibo;

            return View(viewModel);
        }

        [HttpPost]
        public IActionResult UpdateProfile(ProfileViewModel profileViewModel)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { success = false, message = "用户名不能为空.", });
            }

            var userinfo = _context.ApplicationUsers.Find(User.GetUserID());
            userinfo.Address = profileViewModel.Address;
            userinfo.BirthDate = profileViewModel.BirthDate;
            userinfo.Email = profileViewModel.Email;
            userinfo.FullName = profileViewModel.FullName;
            userinfo.Gender = profileViewModel.Gender;
            userinfo.Signature = profileViewModel.Signature;
            userinfo.Intro = profileViewModel.Intro;
            userinfo.PhoneNumber = profileViewModel.PhoneNumber;
            userinfo.QQ = profileViewModel.QQ;
            //userinfo.Status = profileViewModel.Status;
            userinfo.Title = profileViewModel.Title;
            userinfo.WeChat = profileViewModel.WeChat;
            userinfo.Weibo = profileViewModel.Weibo;
            _context.ApplicationUsers.Update(userinfo);
            _context.SaveChanges();
            return Json(new { success = true, message = "更新数据成功.", });
        }
    }
}
